package cn.itsource.xhy.org.controller;

import cn.itsource.xhy.org.domain.Employee;
import cn.itsource.xhy.org.query.EmployeeQuery;
import cn.itsource.xhy.org.service.IEmployeeService;
import cn.itsource.xhy.user.domain.LoginInfo;
import cn.itsource.xhy.user.domain.User;
import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/*@CrossOrigin*/
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PutMapping // put请求添加和修改，在参数中加上@RequestBody注解接收json格式数据，转换成对象
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}") // delete请求，删除，注解后面加上("/{id}")，地址栏中的占位符，  在参数中加上@PathVariable("id")，传递id
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }

    @GetMapping("/{id}") // get请求，查单个，注解后面加上("/{id}")，地址栏中的占位符，   在参数中加上@PathVariable("id")，传递id
    public Employee findById(@PathVariable("id") Long id){
        try {
            return employeeService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/pass/{id}")
    public AjaxResult findAndUpdate(@PathVariable("id") Long id){
        try {
            employeeService.findAndUpdate(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping  // get请求，查所有
    public List<Employee> findAll(){
        try {
            return employeeService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    public QueryResult<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    @PostMapping("/login")
    public AjaxResult Login(@RequestBody Employee employee){
        try {
            // 拿到对象
            LoginInfo loginInfo = employeeService.login(employee);
            // UUID拿到token
            String token = UUID.randomUUID().toString();
            // 将当前用户放在session中
            redisTemplate.opsForValue().set(token,loginInfo,30, TimeUnit.MINUTES);
            HashMap<String, Object> map = new HashMap<>();
            // 将数据装在map
            map.put("token",token);
            // 将敏感数据清空
            loginInfo.setPassword("");
            loginInfo.setSalt("");
            map.put("loginInfo",loginInfo);
            return AjaxResult.me().setResultObj(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

}
